<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item label="计划ID" prop="planId">
            <el-input
                v-model="queryParams.planId"
                placeholder="请输入计划ID"
                clearable
                @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="用户ID" prop="userId">
            <el-input
                v-model="queryParams.userId"
                placeholder="请输入用户ID"
                clearable
                @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item prop="assignType">
            <template #label>
              <el-tooltip content="分配类型: 1-负责人, 2-实施人" placement="top">
                <span>分配类型 <el-icon><InfoFilled /></el-icon></span>
              </el-tooltip>
            </template>
            <el-select v-model="queryParams.assignType" placeholder="请选择分配类型" clearable style="width: 100%;">
              <!-- 修正：v-for 和所有属性都在 el-option 的开始标签内 -->
              <el-option
                  v-for="dict in mission_personnel"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item class="search-buttons-aligned">
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['maintenance:assign:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['maintenance:assign:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['maintenance:assign:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['maintenance:assign:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="assignList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="分配ID" align="center" prop="id" width="80" />
      <el-table-column label="计划ID" align="center" prop="planId" width="100" />
      <el-table-column label="用户ID" align="center" prop="userId" width="100" />
      <el-table-column prop="assignType" align="center" min-width="120px">
        <template #header>
          <el-tooltip content="分配类型: 1-负责人, 2-实施人" placement="top">
            <span>分配类型 <el-icon><InfoFilled /></el-icon></span>
          </el-tooltip>
        </template>
        <template #default="scope">
          <dict-tag :options="mission_personnel" :value="scope.row.assignType"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['maintenance:assign:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['maintenance:assign:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改维护人员分配对话框 -->
    <el-dialog :title="title" v-model="open" width="550px" append-to-body>
      <el-form ref="assignRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="计划ID" prop="planId">
          <el-input v-model="form.planId" placeholder="请输入计划ID" />
        </el-form-item>
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item prop="assignType">
          <template #label>
            <el-tooltip content="分配类型: 1-负责人, 2-实施人" placement="top">
              <span>分配类型 <el-icon><InfoFilled /></el-icon></span>
            </el-tooltip>
          </template>
          <el-select v-model="form.assignType" placeholder="请选择分配类型" style="width: 100%;">
            <!-- 修正：v-for 和所有属性都在 el-option 的开始标签内 -->
            <el-option
                v-for="dict in mission_personnel"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Assign">
import { listAssign, getAssign, delAssign, addAssign, updateAssign } from "@/api/maintenance/assign"
import { InfoFilled } from '@element-plus/icons-vue' // 引入图标

const { proxy } = getCurrentInstance()
const { mission_personnel } = proxy.useDict('mission_personnel')

const assignList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    planId: null,
    userId: null,
    assignType: null,
  },
  rules: {
    planId: [
      { required: true, message: "计划ID不能为空", trigger: "blur" }
    ],
    userId: [
      { required: true, message: "用户ID不能为空", trigger: "blur" }
    ],
    assignType: [
      { required: true, message: "分配类型不能为空", trigger: "change" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询维护人员分配列表 */
function getList() {
  loading.value = true
  listAssign(queryParams.value).then(response => {
    assignList.value = response.rows.map(item => ({
      ...item,
      assignType: item.assignType !== null ? Number(item.assignType) : null
    }));
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    planId: null,
    userId: null,
    assignType: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null
  }
  proxy.resetForm("assignRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  queryParams.value.assignType = null; // 显式重置
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加维护人员分配"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getAssign(_id).then(response => {
    response.data.assignType = response.data.assignType !== null ? Number(response.data.assignType) : null;
    form.value = response.data
    open.value = true
    title.value = "修改维护人员分配"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["assignRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateAssign(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addAssign(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除维护人员分配编号为"' + _ids + '"的数据项？').then(function() {
    return delAssign(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('maintenance/assign/export', {
    ...queryParams.value
  }, `assign_${new Date().getTime()}.xlsx`)
}

getList()
</script>

<style scoped>
/* 统一的表单标签和图标样式 */
.el-form-item__label span {
  display: flex;
  align-items: center;
  white-space: nowrap; /* 防止标签文本换行 */
}
.el-form-item__label .el-icon {
  margin-left: 4px; /* 图标与文字间距 */
  font-size: 14px;
  cursor: help; /* 提示可悬浮 */
}

/* 搜索按钮组的对齐样式，消除默认左边距以更好地对齐 */
.search-buttons-aligned {
  margin-left: 0 !important;
  white-space: nowrap; /* 确保按钮不换行 */
}

/* 表格内长文本截断样式 */
.truncated-text {
  display: block; /* 确保占满宽度 */
  white-space: nowrap; /* 不换行 */
  overflow: hidden; /* 隐藏超出部分 */
  text-overflow: ellipsis; /* 显示省略号 */
  max-width: 100%; /* 限制最大宽度 */
}
</style>
